package org.mbds.android.p2p.threads;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import org.mbds.android.p2p.services.P2pService;

import android.os.AsyncTask;

public class Sender extends AsyncTask<String, String, Boolean>{

	P2pService mService;
	Socket mClient;
	public Sender(P2pService service, Socket client){
		mService = service;
		mClient = client;
	}
	
	
	@Override
	protected Boolean doInBackground(String... params) {
		
		PrintWriter pw  = null;
		String message = "";
		for(int i = 0; i<params.length; i++){
			message += params[i];
		}
		try {
			pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(mClient.getOutputStream())));
			pw.write(message);
			pw.close();
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	protected void onPostExecute(Boolean result){
		mService.sismappMessageSent(result);
	}

}
